package com.vdong.common.oauth.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vdong.common.oauth.common.enums.AuthorizeTypeEnum;
import com.vdong.common.oauth.common.enums.PlatformEnum;
import com.vdong.common.oauth.common.exception.WebBizException;
import com.vdong.common.oauth.common.util.Result;
import com.vdong.common.oauth.entity.dto.PayConfigDTO;
import com.vdong.common.oauth.entity.dto.WechatAuthorizerInfoVO;
import com.vdong.common.oauth.entity.mo.WechatAuthorizerInfo;
import com.vdong.common.oauth.entity.po.PayConfigPO;
import com.vdong.common.oauth.entity.vo.PayConfigVO;
import com.vdong.common.oauth.service.SmsVerificationCodeService;
import com.vdong.common.oauth.service.WechatAuthorizerInfoService;
import com.vdong.trade.trading.facade.PayConfigFacade;
import com.vdong.trade.trading.facade.enums.PayConfigEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Api(value = "授权管理接口", tags = {"授权管理接口"})
@RestController
@RequestMapping("/wechatAuthorizerInfo")
public class WechatAuthorizerInfoController {


    @Autowired
    private SmsVerificationCodeService smsVerificationCodeService;

    @Autowired
    private WechatAuthorizerInfoService wechatAuthorizerInfoService;

    @Reference
    private PayConfigFacade payConfigFacade;

    @ApiOperation(value = "取消授权信息发送短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query", dataType = "string")
    })
    @PostMapping("sendVerificationCodeDelAuth")
    public Result sendVerificationCodeDelAuth(String phone) {
        String code = smsVerificationCodeService.sendVerificationCodeDelAuth(phone, PlatformEnum.WEB);
        return Result.success(code);
    }


    @ApiOperation(value = "根据授权类型获取店铺授权信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorizeType", value = "绑定类型(1=公众号,2序)", paramType = "query", dataType = "int")
    })
    @PostMapping("getAuthorizerInfo")
    public Result<WechatAuthorizerInfoVO> getAuthorizerInfo() {
        AuthorizeTypeEnum authorizeTypeEnum = AuthorizeTypeEnum.getEnum(1);
        WechatAuthorizerInfoVO vo = wechatAuthorizerInfoService.getAuthorizerInfo(-1L, authorizeTypeEnum);
        return Result.success(vo);
    }

    @ApiOperation(value = "解绑公众号")
    @PostMapping("unbundAuthorizerInfo")
    public Result unbundAuthorizerInfo() {
        WechatAuthorizerInfo vo =  wechatAuthorizerInfoService.loadByStoreId(-1L,AuthorizeTypeEnum.Open);
        Boolean result = wechatAuthorizerInfoService.deleteAuth(vo.getAppid());
        return Result.success(result);
    }

    @RequestMapping(value= "/savaOrUpdatePay",method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改支付参数")
    public Result savaOrUpdatePay(@RequestBody PayConfigPO payConfigPO) throws IOException {
        String cert = payConfigPO.getCertName();
        PayConfigDTO payConfigDTO = null;
        payConfigDTO = PayConfigDTO.builder().mchId(payConfigPO.getMchId()).payKey(payConfigPO.getPayKey())
                .createUser(payConfigPO.getUser()).type(PayConfigEnum.OFFICIAL_ACCOUNT.getCode()).build();
        Boolean result = wechatAuthorizerInfoService.savaOrUpdatePay(payConfigDTO);
        return Result.success(result);
    }

    @ApiOperation(value = "保存支付证书")
    @PostMapping("/savePayCert")
    public Result savePayCert(@RequestParam MultipartFile apiclientCertFile) {
        byte[] file;
        String fileName;
        try {
            fileName = apiclientCertFile.getOriginalFilename();
            file = apiclientCertFile.getBytes();
        } catch (IOException e) {
            throw new WebBizException("证书提取失败");
        }
        PayConfigDTO payConfigDTO = PayConfigDTO.builder().certName(fileName).payCert(file).build();
        Boolean result = wechatAuthorizerInfoService.savaOrUpdatePay(payConfigDTO);
        return Result.success(result);
    }

    @RequestMapping(value= "/selectPayConfig",method = RequestMethod.POST)
    @ApiOperation(value = "查询支付参数")
    public Result selectPayConfig(){
        PayConfigDTO payConfigDTO = wechatAuthorizerInfoService.selectPayConfig();
        PayConfigVO payConfigVO = PayConfigVO.builder().certName(payConfigDTO.getCertName()).mchId(payConfigDTO.getMchId())
                .payKey(payConfigDTO.getPayKey()).build();
        return Result.success(payConfigVO);
    }


}
